package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.RecommendUser;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;
import java.util.Map;

@DubboService
public class RecommendUserApiImpl  implements RecommendUserApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    //查询今日佳人
    public RecommendUser queryWithMaxScore(Long toUserId) {

        //根据toUserId查询，根据评分score排序，获取第一条
        //构建Criteria
        Criteria criteria = Criteria.where("toUserId").is(toUserId);
        //构建Query对象
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("score")))
                .limit(1);
        //调用mongoTemplate查询

        return mongoTemplate.findOne(query,RecommendUser.class);
    }

    @Override
    public PageResult queryRecommendUserList(Integer page, Integer pagesize, Long toUserId) {
        //构建查询userid的条件
        Query query = Query.query(Criteria.where("toUserId").is(toUserId));
        //在mongo里面查询总记录数
        long count2 = mongoTemplate.count(query, RecommendUser.class);
        //分页查询
        query.skip((page - 1)*pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("score")));
        List<RecommendUser> recommendUsers = mongoTemplate.find(query, RecommendUser.class);
        String count1 = count2+"";
        Integer count = Integer.valueOf(count1);
        return new  PageResult(page,pagesize,count,recommendUsers);
    }

    /**
     * 查询缘分值
     * @param userId
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser queryScore(Long userId, Long toUserId) {
        Query query = new Query(Criteria.where("userId").is(userId)
        .and("toUserId").is(toUserId));
        RecommendUser recommendUser = mongoTemplate.findOne(query, RecommendUser.class);
        if (recommendUser == null) {
            recommendUser = new RecommendUser();
            //如果为空，构建假数据
            recommendUser.setScore(95D);
            recommendUser.setUserId(userId);
            recommendUser.setToUserId(toUserId);
        }
        return recommendUser;
    }

    @Override
    public List<RecommendUser> queryCardsList(Long userId, int i) {
        //根据userlike表查询有无喜欢与不喜欢的，进行排除
        Query query = Query.query(Criteria.where("userId").is(userId));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        //取出userlikes集合里面的likeuserid
        List<Long> likeUserIds = CollUtil.getFieldValues(userLikes, "likeUserId", Long.class);

        //根据条件查询recommentuser表，列出推荐的人
        Criteria nin = Criteria.where("toUserId").is(userId).and("userId").nin(likeUserIds);
        TypedAggregation<RecommendUser> recommendUserTypedAggregation = TypedAggregation.newAggregation(RecommendUser.class,
                Aggregation.match(nin),
                Aggregation.sample(i));
        AggregationResults<RecommendUser> aggregate = mongoTemplate.aggregate(recommendUserTypedAggregation, RecommendUser.class);
        return aggregate.getMappedResults();
    }

    @Override
    public Map<Long, RecommendUser> queryFateValue(Long toUserId, List<Long> userIds) {
        Query query = Query.query(Criteria.where("toUserId").is(toUserId)
        .and("userId").in(userIds));
        List<RecommendUser> userList = mongoTemplate.find(query, RecommendUser.class);
        Map<Long, RecommendUser> userId = CollUtil.fieldValueMap(userList, "userId");
        return userId;
    }
}